<?php

namespace App\Service\UserCenter;

use App\Constants\ErrorCode;
use App\Helpers\ConfigUtil;
use App\Model\UserCoin;
use App\Model\UserProfilePersonal;
use App\Model\Withdraw;
use App\Service\BaseService;

/**
 * 提现详情信息
 */
class WithdrawInfoService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $version = $this->request->input('version', '');
        $userCoin = UserCoin::getUserInfo($userId);
        $personInfo = UserProfilePersonal::getProfile($userId);

//        if (!UserProfilePersonal::isFaceVerified($personInfo)) {
//            log_user_result($userId, "NO_FACE_VRFY");
//            return $this->setError(ErrorCode::NO_ID_VRFY, '请先完成实名认证～（如在认证审核中，请通过审核后再提现哦！）');
//        }

        if (!$personInfo) {
            return $this->setError(ErrorCode::WITHDRAW_INFO_EMPTY, '提现资料还未提交~');
        }
        $incomeCoin = $userCoin['income_coin'] ?? 0;
        //最大可提现
        $withdrawLmt = Withdraw::getMaxRmbLimit($userId, $incomeCoin);
        $feeStart = Withdraw::checkFeeStart($version);
        $feePercent = !$feeStart ? 0.0 : ConfigUtil::getWithdrawFeeRate();
        $text = !$feeStart ? "提现手续费6.5%（已免除手续费）" : "提现手续费6.5%";

        $oldRule = "提现规则：\n1、每日可提现一次，单次最高可提现2000元人民币；\n2、非首次提现金额需为100元的整数倍；\n3、首次提现任意金额即可提现，非首次提现需满200元方可提现\n4、提现到账时间: 每日18:00之前提现申请，当日23:00前到账。每日18:00之后提现申请，将在次日23:00前到账（非工作日及特殊情况，提现到账时间将适度顺延）";
        $newRule = "提现规则：\n1、每日可提现三次，单次最高可提现2000元人民币；\n2、提现到账时间:每日18:00之前提现申请，当日23:00前到账。每日18:00之后提现申请，将在次日23:00前到账(非工作日及特殊情况，提现到账时间将适度顺延)；\n3、收益提现时平台将收取6.5%的手续费。";
        $withdrawList = Withdraw::WITHDRAW_LIST;

        //是否首次提现，是否出现秒到账套餐
         $lastWithdraw = Withdraw::checkFirstV2((int)$userId);
         if ($lastWithdraw === false || $lastWithdraw !== null) {
             array_shift($withdrawList);
         }

        return [
            'alipay_account' => $personInfo['alipay_account'] ?? '',
            'income_coin' => $incomeCoin,
            'withdraw_limit' => floor($withdrawLmt),
            'real_name' => $personInfo['real_name'] ?? '',
            'withdraw_unit' => Withdraw::FIRST_WITHDRAW_UNIT,
            'rules' => $this->curVersion >= '1.0.0' ? $newRule : $oldRule,
            // 'account_type' => $personInfo['alipay_account'] ? 0 : 1,
            // 'account' => $personInfo['alipay_account'] ?: $personInfo['bank_card'],
            'account_type' => $personInfo['bank_card'] ? 1 : 0,
            'account' => $personInfo['bank_card'] ?: $personInfo['alipay_account'],
            'fee_tip' => $text,
            'fee_percent' => $feePercent,
            'withdraw_list' => $withdrawList
        ];
    }
}
